/**
 * 屠宰记录
 * @constructor
 */
function SlaughterRecord() {
}

SlaughterRecord.init = function () {

    $('#tableList').bootstrapTable({
        url: '/slaughterRecord/tableSearch',
        method: 'post',
        striped: true, //是否显示行间隔色
        toolbar: "#toolbar",
        sidePagination: "true",
        dataField: "data",//这是返回的json数组的key.默认是"rows".这里只有前后端约定好就行
        showToggle: true, //是否显示详细视图和列表视图的切换按钮
        striped: true, // 是否显示行间隔色
        //search : "true",
        uniqueId: "id",
        pageList: [5, 10, 20, 50, 100],//可选择单页记录数
        pageSize: "10",
        pagination: true, // 是否分页
        sortable: true, // 是否启用排序
        sidePagination: 'server', // 设置为服务器端分页     客户端：client
        search: false,
        showColumns: true,
        showRefresh: true,
        showExport: true,
        //得到查询的参数
        queryParams: function (params) {
            //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
            var query_params = {
                //********table的参数 不变
                pageSize: params.limit,                         //页面大小
                pageIndex: (params.offset / params.limit) + 1,   //页码
                sort: params.sort,      //排序列名
                sortOrder: params.order, //排位命令（desc，asc）
                //********
                //获取查询的对象，往下加入
                sourceAddress: $('#search_SourceAddress').val(), // 请求时向服务端传递的参数
                timeInterval: $('#search_EntryDate').val(), // 请求时向服务端传递的参数
            };
            return query_params;
        },
        columns: [
            {
                //field: 'Number',//可不加
                title: '序号',//标题  可不加
                align: "center",
                width: 40,
                formatter: function (value, row, index) {
                    var pageSize = $('#tableList').bootstrapTable('getOptions').pageSize;//通过表的#id 可以得到每页多少条
                    var pageNumber = $('#tableList').bootstrapTable('getOptions').pageNumber;//通过表的#id 可以得到当前第几页
                    return pageSize * (pageNumber - 1) + index + 1;//返回每条的序号： 每页条数 * （当前页 - 1 ）+ 序号
                }
            },
            {field: 'enterpriseName', title: '企业名称', align: 'center'},
            {field: 'entryDate', title: '进厂日期', align: 'center'},
            {field: 'sourceAddress', title: '来源地址', align: 'center'},
            {field: 'ownerName', title: '畜主名称', align: 'center'},
            {field: 'buyer', title: '购买人', align: 'center'},
            {field: 'type', title: '种类', align: 'center'},
            {field: 'number', title: '进厂数量', align: 'center'},
            {field: 'quarantineNo', title: '检疫证号', align: 'center'},
            {field: 'slaughterDate', title: '屠宰日期', align: 'center'},
            {field: 'slaughterNumber', title: '屠宰数量', align: 'center'},
            {
                title: '操作',
                align: 'center',
                formatter: function (value, row, index) {
                    var optionHtml = "";
                    if (row.outState == 0) {
                        optionHtml = optionHtml + "<a class=\"btn btn-success btn-xs\"  href=\"javascript:void(0)\"  onclick='actionRecord(" + JSON.stringify(row) + ",\"edit\")' title=\"出厂\" ><i class=\"fa fa-edit\"></i>出厂</a>";
                    } else {
                        optionHtml = optionHtml + "<a class=\"btn btn-danger btn-xs\"  title=\"已出厂\" ><i class=\"fa fa-edit\"></i>已出厂</a>";
                    }
                    return optionHtml;
                }
            }
        ]
    });
}

//查询方法****************************
function doSearch() {
    // $('#tableList').bootstrapTable(('refresh')); // 很重要的一步，刷新url！
    $("#tableList").bootstrapTable("refreshOptions", {pageNumber: 1});
}
laydate.render({
    elem: '#search_EntryDate',
    range: true,
});
//重置****************************
function doRest() {
    $("#search_SourceAddress").val("");
    $("#search_EntryDate").val("");
    // $('#tableList').bootstrapTable(('refresh')); // 很重要的一步，刷新url！
    $("#tableList").bootstrapTable("refreshOptions", {pageNumber: 1});
}

//增加方法****************************
var Action = "";
var slaughterId = 0;
var slaughterEarMark = "";
var entryRecordId = ""

//出厂数据保存****************************
function saveRecord() {
    if(!validateForm()) return false;
    var postdata = {
        slaughterId: slaughterId,
        entryRecordId: entryRecordId,
        action: Action,
        meatInspectionNo: $("#text_MeatInspectionNo").val(),
        flowAddress: $("#text_FlowAddress").val(),
        sellerName: $("#text_SellerName").val(),
    };
    //发送异步请求到后台保存用户数据
    $.ajax({
        url: "/factoryRecord/save",
        type: "post",
        data: JSON.stringify(postdata),
        contentType: "application/json",
        dataType: "json",
        success: function (data) {
            if (data.code === "200") {
                $('#myModalAdd').modal('hide')
                doSearch();
                swal("操作成功!", "请继续操作", "success");
            } else {
                swal("操作失败!", "请重试", "error");
            }
        }
    });
}

function actionRecord(data, type) {
    //修改
    slaughterId = data.slaughterId;
    entryRecordId=data.entryRecordId;
    Action = "edit";
    $("#myform")[0].reset();
    $("#save").removeAttr("disabled");
    $('#myModalAdd').modal('show')
}

//表单验证
function validateForm() {
    //validate方法参数可选
    return $("#myform").validate({
        rules: {
            text_FlowAddress: {
                required: true,//必填
                minlength: 1, //最少1个字符
            },
            text_SellerName: {
                required: true,//必填
                minlength: 1, //最少1个字符
            },
            text_MeatInspectionNo: {
                required: true,//必填
                minlength: 1, //最少1个字符
            },
        },
        //错误信息提示
        messages: {
            text_MeatInspectionNo: {
                required: "(必填)",
                minlength: "最少1位",
            },
            text_FlowAddress: {
                required: "(必填)",
                minlength: "最少1位",
            },
            text_SellerName: {
                required: "(必填)",
                minlength: "最少1位",
            },
        },
    }).form();
}